package com.lz.manage.service.impl;

import com.lz.manage.mapper.StatisticsMapper;
import com.lz.manage.mapper.TagInfoMapper;
import com.lz.manage.model.domain.TagInfo;
import com.lz.manage.model.enums.NFileTypeEnum;
import com.lz.manage.model.statistics.ro.StatisticsRo;
import com.lz.manage.model.statistics.vo.PieStatisticsVo;
import com.lz.manage.service.IStatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 统计类
 *
 * @Project: networkStorage
 * @Author: YY
 * @CreateTime: 2025-07-17  19:52
 * @Version: 1.0
 */
@Service
public class StatisticsServiceImpl implements IStatisticsService {
    @Resource
    private StatisticsMapper statisticsMapper;

    @Resource
    private TagInfoMapper tagInfoMapper;

    @Override
    public PieStatisticsVo fileType() {
        List<StatisticsRo> statisticsRoList = statisticsMapper.fileType();
        //构造返回结果
        PieStatisticsVo pieStatisticsVo = new PieStatisticsVo();
        List<String> names = new ArrayList<>();
        List<PieStatisticsVo.Data> datas = new ArrayList<>();
        for (StatisticsRo statisticsRo : statisticsRoList) {
            String name = statisticsRo.getName();
            //获取枚举，根据枚举值获取枚举名
            Optional<NFileTypeEnum> enumByValue = NFileTypeEnum.getEnumByValue(name);
            if (enumByValue.isPresent()) {
                name = enumByValue.get().getLabel();
            } else {
                name = "未知";
            }
            names.add(name);
            PieStatisticsVo.Data data = new PieStatisticsVo.Data();
            data.setName(name);
            data.setValue(statisticsRo.getTotal());
            datas.add(data);
        }
        pieStatisticsVo.setDatas(datas);
        pieStatisticsVo.setNames(names);
        return pieStatisticsVo;
    }

    @Override
    public PieStatisticsVo fileTag() {
        List<StatisticsRo> statisticsRoList = statisticsMapper.fileTag();
        PieStatisticsVo pieStatisticsVo = new PieStatisticsVo();
        List<String> names = new ArrayList<>();
        List<PieStatisticsVo.Data> datas = new ArrayList<>();

        for (StatisticsRo statisticsRo : statisticsRoList) {
            String name = statisticsRo.getName();
            if (name == null) {
                name = "未分类";
            } else {
                TagInfo tagInfo = tagInfoMapper.selectTagInfoByTagId(Long.valueOf(name));
                name = tagInfo.getTagName();
            }
            PieStatisticsVo.Data data = new PieStatisticsVo.Data();
            data.setName(name);
            data.setValue(statisticsRo.getTotal());
            datas.add(data);
        }
        pieStatisticsVo.setDatas(datas);
        pieStatisticsVo.setNames(names);
        return pieStatisticsVo;
    }

    @Override
    public PieStatisticsVo fileSuffix() {
        List<StatisticsRo> statisticsRoList = statisticsMapper.fileSuffix();
        PieStatisticsVo pieStatisticsVo = new PieStatisticsVo();
        List<String> names = new ArrayList<>();
        List<PieStatisticsVo.Data> datas = new ArrayList<>();

        for (StatisticsRo statisticsRo : statisticsRoList) {
            names.add(statisticsRo.getName());
            PieStatisticsVo.Data data = new PieStatisticsVo.Data();
            data.setName(statisticsRo.getName());
            data.setValue(statisticsRo.getTotal());
            datas.add(data);
        }
        pieStatisticsVo.setDatas(datas);
        pieStatisticsVo.setNames(names);
        return pieStatisticsVo;
    }
}
